using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

public partial class gallery_selectalbumphotoeffects : System.Web.UI.Page
{
	protected Guid guid;

	protected void Page_Load(object sender, EventArgs e)
	{
		guid = new Guid(Request.QueryString["AlbumId"]);

		if (!Page.IsPostBack && !Page.IsCallback)
			BindPage();

		btnSave.Click += new EventHandler(btnPhotoEffect_Click);
	}

	#region Event Handlers

	/// <summary>
	/// Saves the album
	/// </summary>
	private void btnPhotoEffect_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			Album album = Album.GetById(guid);

			album.PhotoEffects.Clear();

			if (ddlPhotoEffect1.SelectedIndex != 0)
				album.PhotoEffects.Add(PhotoEffect.GetById(new Guid(ddlPhotoEffect1.SelectedValue)));
			if (ddlPhotoEffect2.SelectedIndex != 0)
				album.PhotoEffects.Add(PhotoEffect.GetById(new Guid(ddlPhotoEffect2.SelectedValue)));
			if (ddlPhotoEffect3.SelectedIndex != 0)
				album.PhotoEffects.Add(PhotoEffect.GetById(new Guid(ddlPhotoEffect3.SelectedValue)));
			if (ddlPhotoEffect4.SelectedIndex != 0)
				album.PhotoEffects.Add(PhotoEffect.GetById(new Guid(ddlPhotoEffect4.SelectedValue)));
			if (ddlPhotoEffect5.SelectedIndex != 0)
				album.PhotoEffects.Add(PhotoEffect.GetById(new Guid(ddlPhotoEffect5.SelectedValue)));

			album.Save();
		}
	}

	#endregion

	#region Data Binding

	private void BindPage()
	{
		Album album = Album.GetById(guid);

		BindDropDown(ddlPhotoEffect1, album, 0);
		BindDropDown(ddlPhotoEffect2, album, 1);
		BindDropDown(ddlPhotoEffect3, album, 2);
		BindDropDown(ddlPhotoEffect4, album, 3);
		BindDropDown(ddlPhotoEffect5, album, 4);
	}

	private void BindDropDown(DropDownList dropdownlist, Album album, int index)
	{
		if (index >= album.PhotoEffects.Count)
			BindDropDown(dropdownlist, null);
		else
			BindDropDown(dropdownlist, album.PhotoEffects[index]);
	}

	private void BindDropDown(DropDownList dropdownlist, PhotoEffect photoeffect)
	{
		dropdownlist.Items.Add(new ListItem("-- None --", Guid.Empty.ToString()));

		foreach (PhotoEffect effect in PhotoEffect.Items)
			dropdownlist.Items.Add(new ListItem(effect.Title, effect.Id.ToString()));

		if (photoeffect != null)
			dropdownlist.SelectedValue = photoeffect.Id.ToString();
	}

	#endregion


}
